package com.irdstudio.basic.sequence.service;

import com.irdstudio.basic.sequence.service.util.PatternDescriptorHelper;

/**
 * @description:PatternDescriptor的默认实现
 * @date:2019年2月13日 下午5:41:07
 * @author:hengyh
 */
public class DefaultPatternDescriptor implements PatternDescriptor {

	private static final long serialVersionUID = -1944464837802275293L;
	private String originPatternString;
    private String resolvedPatternString;
    private String split = PatternDescriptor.DEFAULT_SPLIT;

    /* OPTIONAL */
    private Long seqMaxValue;
    private Integer seqLength;

    /* PATTERN NAMES */
    public static final String SPLIT = PatternDescriptor.DEFAULT_SPLIT;
    public static final String YEAR_4 = SPLIT + "YEAR" + SPLIT;
    public static final String YEAR_2 =  SPLIT + "YE" + SPLIT;
    public static final String MONTH =  SPLIT + "MONTH" + SPLIT;
    public static final String DAY =  SPLIT + "DAY" + SPLIT;
    public static final String HOUR =  SPLIT + "HOUR" + SPLIT;
    public static final String MIN =  SPLIT + "MIN" + SPLIT;
    public static final String SECOND =  SPLIT + "SECOND" + SPLIT;
    public static final String SEQ = SPLIT + PatternDescriptor.DEFAULT_SEQ_NAME + SPLIT;

    public DefaultPatternDescriptor(String unResolvedPatternString) {
        this.originPatternString = unResolvedPatternString;
    }

    public DefaultPatternDescriptor(String resolvedPatternString, Long seqMaxValue) {
        this.resolvedPatternString = resolvedPatternString;
        this.seqMaxValue = seqMaxValue;
        this.seqLength = String.valueOf(seqMaxValue).length();
    }
    
    public DefaultPatternDescriptor(String resolvedPatternString, Integer seqLength) {
        this.resolvedPatternString = resolvedPatternString;
        this.seqLength = seqLength;
        this.seqMaxValue = PatternDescriptorHelper.getMaxValueFromSeqLength(seqLength);
    }

    public DefaultPatternDescriptor(String resolvedPatternString, String split, Long seqMaxValue, Integer seqLength) {
        this.resolvedPatternString = resolvedPatternString;
        this.split = split;
        this.seqMaxValue = seqMaxValue;
        this.seqLength = seqLength;
    }
    
    /**
     * @description:通过原始pattern和序列号长度构造pattern
     * @param resolvedPatternString
     * @param seqLength
     */
    public static PatternDescriptor of(String unResolvedPatternString, Integer seqLength) {
    	DefaultPatternDescriptor descriptor = new DefaultPatternDescriptor(unResolvedPatternString);
    	descriptor.setSeqLength(seqLength);
		return descriptor;
	}

    @Override
    public String getResolvedPatternString() {
        return resolvedPatternString;
    }

    @Override
    public String getOriginPatternString() {
        return originPatternString;
    }

    @Override
    public String getSplit() {
        return split;
    }

    @Override
    public String getSeqName() {
        return SEQ;
    }

    @Override
    public Long getSeqMaxValue() {
        return seqMaxValue;
    }

    @Override
    public Integer getSeqLength() {
        return seqLength;
    }

    public void setResolvedPatternString(String resolvedPatternString) {
        this.resolvedPatternString = resolvedPatternString;
    }

    public void setSplit(String split) {
        this.split = split;
    }

    public void setOriginPatternString(String originPatternString) {
        this.originPatternString = originPatternString;
    }

    public void setSeqLength(Integer seqLength) {
        this.seqLength = seqLength;
    }

    public void setSeqMaxValue(Long seqMaxValue) {
        this.seqMaxValue = seqMaxValue;
    }
}
